package com.cgx.tablet.beeper.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;

import com.google.gson.stream.MalformedJsonException;
import com.cgx.tablet.beeper.adapter.viewholder.RecycleViewHolder;

import java.util.List;

/**
 * @author Created by GammaQ.
 * @version V1.0
 * @FileName: vwoof.com.xundaobaoclient.adapter.BaseRecycleViewAdapter.java
 * @date:2016-04-26 15:39
 */
public abstract class BaseRecycleViewAdapter<M, H extends RecycleViewHolder<M>> extends RecyclerView.Adapter<H> {
    private List<M> mData;

    public BaseRecycleViewAdapter(Context context, @NonNull List<M> mData) {
        this.mData = mData;
    }

    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }


    @Override
    public void onBindViewHolder(H holder, int position) {
        try {
            holder.Bind(mData.get(position));
        } catch (MalformedJsonException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取position位置的item数据
     *
     * @param position
     * @return
     */
    public M getItem(int position) {
        return mData.get(position);
    }

    /**
     * 更新数据
     *
     * @param mData
     */
    public void setData(@NonNull List<M> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }
}